package com.bestv.search.spider.service.impl;

import com.bestv.search.spider.dao.PersonDao;
import com.bestv.search.spider.model.Person;
import com.bestv.search.spider.model.Product;
import com.bestv.search.spider.model.uid.UID;
import com.bestv.search.spider.service.PersonManager;

import java.util.Date;
import java.util.List;

public class PersonManagerImpl implements PersonManager {
    private PersonDao personDao;

    @Override
    public Person getById(String personId, String source) {
        return personDao.getById(personId, source);
    }

    @Override
    public void saveList(List<Person> persons) {
        for (Person person : persons) {
            Person personInDB = personDao.getById(person.getPersonId(), person.getSource());
            if (personInDB == null) {
                personInDB = person;
                personInDB.setId(new Long(UID.getUID(personInDB)));
                personInDB.setUpdateTime(new Date());
                personDao.save(personInDB);
            }
        }
    }

    @Override
    public void updateList(List<Person> persons) {
        for (Person person : persons) {
            personDao.update(person);
        }
    }

    @Override
    public void update(Person person) {
        personDao.update(person);
    }

    @Override
    public List<Person> getByUpdateTime(Date date) {
        return personDao.getByUpdateTime(date);
    }

    @Override
    public List<Person> getAll() {
        return personDao.getAll();
    }

    @Override
    public List<Person> getByNullAvatar_Iqiyi() {
        return personDao.getByNullAvatar_Iqiyi();
    }

    @Override
    public List<Product> getProductsByPersonId(String personeId) {
        return personDao.getProductsByPersonId(personeId);
    }

    public PersonDao getPersonDao() {
        return personDao;
    }

    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }
}
